package com.pets.springboot_petshop.controller;

import com.pets.springboot_petshop.entity.Petproduct;
import com.pets.springboot_petshop.pojo.Result;
import com.pets.springboot_petshop.service.PetproductServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: wxk
 * @Email: 2089967794@qq.com
 * @Date: 2024/10/22 上午2:29 周二
 * @Description: 宠物活体控制层
 */
@RestController
@RequestMapping("/Petproduct")
public class PetproductController {
    @Autowired
    private PetproductServices petproductServices;
    //id查询
    @GetMapping("select")
    private Result select(@RequestParam int id) {
        Petproduct petproduct = petproductServices.select(id);
        if (petproduct != null) {
            return Result.success(petproduct);
        }else {
            return Result.error("获取失败");
        }
    }
    //姓名模糊查询
    @GetMapping("selectname")
    private Result selectname(@RequestParam String name) {
        List<Petproduct> petproducts = petproductServices.selectname(name);
        if (petproducts != null) {
            return Result.success(petproducts);
        }else {
            return Result.error("获取失败");
        }
    }
   //user_id查询
    @GetMapping("selectuser_id")
    private Result selectuser_id(@RequestParam int user_id) {
        List<Petproduct> petproducts = petproductServices.selectuser_id(user_id);
        if (petproducts != null) {
            return Result.success(petproducts);
        }else {
            return Result.error("获取失败");
        }
    }
    //pettype_id查询
    @GetMapping("selectpettype_id")
    private Result selectpettype_id(@RequestParam int pettype_id) {
        List<Petproduct> petproducts = petproductServices.selectpettype_id(pettype_id);
        if (petproducts != null) {
            return Result.success(petproducts);
        }else {
            return Result.error("获取失败");
        }
    }

    //查询所有
    @GetMapping("/findAll")
    private Result findAll() {
        List<Petproduct> petproducts = petproductServices.findAll();
        if(petproducts == null || petproducts.isEmpty()){
            return Result.error("获取失败");
        }else {
            return Result.success(petproducts);
        }
    }
    //添加
    @PostMapping("add")
    private Result add(@RequestBody Petproduct petproduct) {
        if (petproductServices.add(petproduct)>0){
            return Result.success("新增成功");
        }else {
            return Result.error("获取失败");
        }
    }

    //删除
    @PostMapping("delete")
    private Result delete(@RequestParam Integer id){
        if (petproductServices.delete(id)>0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
    //修改数据
    @PostMapping("update")
    private Result update(@RequestBody Petproduct petproduct){
        if (petproductServices.update(petproduct)>0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }
    //修改数据2
    @PostMapping("updateuser_id")
    private Result updateuser_id(@RequestBody Petproduct petproduct){
        if (petproductServices.updateuser_id(petproduct)>0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }
}
